<?php

namespace Admin\Controller;

class SetController extends CommonController {

	 
  public function  index(){
	  
	    $act=I('act','');
		$id=I('id',0,'intval');

		if($act=='add')
		{
			
		  $wname=I('post.swname','');
		  
		  $wlogo=I('post.wlogo','');
		  
		  $wxlogo=I('post.wxlogo','');

		  $url=I('post.surl','');
 
		  $description=I('post.sdescription','');

		  
				  $admin_arr=array(
				  
				  'wname'=>$wname,
				  
				  'wlogo'=>$this->handle_pic($id,'wlogo',$wlogo,'wlogo_'),
				  
				  'wxlogo'=>$this->handle_pic($id,'wxlogo',$wxlogo,'wxlogo_'),
			
				  'url'=>$url,
 
				  'description'=>$description,
				  'ename'=>$_SESSION['admin_info']['ename'],
				  );

	 

			  if(!empty($id)){

				  $editId=M('website')->where(array('id'=>$id))->save($admin_arr);

				  !empty($editId)?$this->success(L('_DO_SUCCEED_'),U('Set/index')):$this->error(L('_NO_EDIT_'),U('Set/index'));
				  
				  exit;
		  

			  }else{

				  

				 $lastId=M('website')->add($admin_arr); 

				 !empty($lastId)?$this->success(L('_DO_SUCCEED_'),U('Set/index')):$this->error(L('_NO_EDIT_'),U('Set/index'));

				 exit;
			  }
		
		}
	  $website_arr=M('website')->where(array('ename'=>$_SESSION['admin_info']['ename']))->find();
	  $this->website=$website_arr;
	  $this->display();
 
  }

 

  /*
  *
  *
  *  更改登录密码
  *
  */

  public function edit_pwd(){

	  

	  $id=I('post.id',0,'intval');

	  

	  if(!empty($id)){

		  

		 $mpass=I('post.mpass','');

		 $newpass=I('post.newpass','');

		 $renewpass=I('post.renewpass','');

		 $newpass!==$renewpass?message(2,L('_NEQ_PWD_')):'';

		 !is_empty($mpass,$newpass,$renewpass)?message(2,L('_IS_EMPTY_')):'';

		 

		 $mpass==$newpass?message(2,L('_OLD_EQ_NEW_')):'';

		 

		 $mpass_arr=M('admin')->where(array('aname'=>$_SESSION['admin_info']['aname'],'apwd'=>md5($mpass.C('SALT'))))->find();

		 

		 !empty($mpass_arr)?'':message(2,L('_ERR_OLDPWD_'));

		 

		 $editId=M('admin')->where(array('aname'=>$_SESSION['admin_info']['aname'],'apwd'=>md5($mpass.C('SALT'))))->save(array('apwd'=>md5($newpass.C('SALT'))));

		 

		 !empty($editId)?message(1,L('_DO_SUCCEED_'),U('Log/quit')):message(2,L('_NO_EDIT_'));

		 

		 

	  }

	  

	  $this->display();

  }
  
    /*-----------------------------公共函数---------------------------------------*/ 
	
	/*
	* 处理pic
	*
	*/
	public function handle_pic($id,$pic_field,$post_field,$prefix){
		
		$old_pic_arr=M('website')->field($pic_field)->where(array('id'=>$id))->find();
		
		
		
		  if(!empty($id))
		  {
			
				
				if($old_pic_arr[$pic_field]!==$post_field)
				{
				
					
					!empty($old_pic_arr[$pic_field])?unlink($old_pic_arr[$pic_field]):'';
					
					 return $this->base64_upload($post_field,$prefix); 
					
				}else{
					
					return $old_pic_arr[$pic_field];
				}
				
				
				
			}else{
				
				
				    return $this->base64_upload($post_field,$prefix); 
				
			}
	  
		
		
	}
	
	
	
	
	 //上传图片
	public  function base64_upload($base64,$type='') {
			 
        $base64_image = str_replace(' ', '+', $base64);//post的数据里面，加号会被替换为空格，需要重新替换回来，如果不是post的数据，则注释掉这一行
       
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){
		
        //匹配成功
			if($result[2] == 'jpeg'){
				
				$image_name = $type.date('YmdHis',time()).'.jpg'; //纯粹是看jpeg不爽才替换的
				
			}else{
				$image_name = $type.date('YmdHis',time()).'.'.$result[2];
			}
			
           $image_file = "./Uploads/{$image_name}";//服务器文件存储路径
        
			if (file_put_contents($image_file, base64_decode(str_replace($result[1], '', $base64_image)))){
				
				return $image_file;
				
			}else{
				 
				return false;
			}
			
        }
		else
		{
			
           return false;
        }
     }
  
  

}



 